package com.shut.vehicle.controller.result;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.Page;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 卢斌
 */
@Getter
@Setter
@NoArgsConstructor
public class PageResult implements Serializable {

    private List<?> list;

    private Integer pageNum;

    private Integer pageSize;

    private Long total;

    private Integer pages;

    public PageResult(List<?> listParam) {
        if (listParam == null) {
            list = new ArrayList<>();
            pageNum = 0;
            pageSize = 0;
            total = 0L;
            pages = 0;
        } else {
            if (listParam instanceof Page) {
                Page<?> page = (Page<?>) listParam;
                this.list = page.getResult();
                this.pages = page.getPages();
                this.pageNum = page.getPageNum();
                this.pageSize = page.getPageSize();
                this.total = page.getTotal();
            } else {
                this.list = listParam;
                this.pages = 1;
                this.pageNum = 1;
                this.pageSize = listParam.size();
                this.total = Integer.valueOf(listParam.size()).longValue();
            }
        }
    }

    public PageResult(List<? extends Serializable> listParam, JSONObject jsonModel) {
        if (listParam == null) {
            list = new ArrayList<>();
            pageNum = 0;
            pageSize = 0;
            total = 0L;
            pages = 0;
        } else {
            if (listParam instanceof Page) {
                Page<? extends Serializable> page = (Page<? extends Serializable>) listParam;
                this.list = Result.filterList(page.getResult(), jsonModel);
                this.pages = page.getPages();
                this.pageNum = page.getPageNum();
                this.pageSize = page.getPageSize();
                this.total = page.getTotal();
            } else {
                this.list = listParam;
                this.pages = 1;
                this.pageNum = 1;
                this.pageSize = listParam.size();
                this.total = Integer.valueOf(listParam.size()).longValue();
            }
        }
    }
}
